package com.alm.token.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alm.token.R;
import com.alm.token.common.DialogHelper;
import com.alm.token.ui.activity.FragmentBackHolderActivity;

/**
 * Created by lidengzhou on 17/6/12.
 */

public interface BaseBehavior {

    void showToast(@StringRes int resId);

    void showToast(String msg);

    void showWaitDialog();

    void showWaitDialog(@StringRes int resId);

    void showWaitDialog(String msg);

    void showUpdateAlertDialog(Context context, String title,
                               String message, String positiveMsg, String negativeMsg, MaterialDialog.SingleButtonCallback callback);

    void hideUpdateAlertDialog();

    MaterialDialog showProgressDialog(Context context, String message, MaterialDialog.SingleButtonCallback callback);

    void showInformationDialog(Context context,String title,
                               String message, String positiveText, String negativeText, MaterialDialog.SingleButtonCallback callback);

    void hideInformationDialog();


    void hideProgressDialog();

    void hideWaitDialog();

    void startActivity(Class<?> t);

    void startActivity(Class<?> t, Bundle extra);

    void startActivityForResult(Class<?> t, int requestCode);

    void startActivityForResult(Class<?> t, int requestCode, Bundle extra);

    void startFragment(Class<?> t);

    void startFragment(Class<?> t, Bundle extra);

    void startFragmentForResult(Class<?> t, int requestCode);

    void startFragmentForResult(Class<?> t, int requestCode, Bundle extra);

    void handleError(int code, String message);

    void hideKeyboard();


    class SimpleBehavior implements BaseBehavior {

        private MaterialDialog materialDialog = null;

        private MaterialDialog infoDialog = null;

        private MaterialDialog updateAlertDialog = null, progressDialog = null, informationDialog = null;


        private Context context;

        private Activity activity = null;
        private Fragment fragment = null;
        private Toast toast = null;

        public SimpleBehavior(@NonNull Activity activity) {
            this.activity = activity;
            context = activity;
        }

        public SimpleBehavior(@NonNull Fragment fragment) {
            this.fragment = fragment;
            context = fragment.getActivity();
        }

        @Override
        public void showToast(@StringRes int resId) {
            if (context != null) {
                showToast(context.getString(resId));
            }
        }

        @Override
        public void showToast(String msg) {
            if (context != null) {
                if (toast == null) {
                    toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                }
                toast.setText(msg);
                toast.show();
            }
        }

        @Override
        public void showWaitDialog() {
            if (context != null) {
                showWaitDialog(context.getString(R.string.network_loading_hint));
            }
        }

        @Override
        public void showWaitDialog(@StringRes int resId) {
            if (context != null) {
                showWaitDialog(context.getString(resId));
            }
        }

        @Override
        public void showWaitDialog(String msg) {
            if (context != null) {
                if (materialDialog == null) {
                    materialDialog = DialogHelper.getWaitDialog(context, msg);
                    materialDialog.setCanceledOnTouchOutside(false);
//                    materialDialog.getWindow().setBackgroundDrawableResource(R.color.colorDialogBgd);//设置Dialog背景透明效果
                } else {
                    materialDialog.setContent(msg);
                }
                materialDialog.show();
            }

        }

        @Override
        public void showUpdateAlertDialog(Context context, String title, String message, String positiveMsg, String negativeMsg, MaterialDialog.SingleButtonCallback callback) {
            if (context != null) {
                if (updateAlertDialog == null) {
                    updateAlertDialog = DialogHelper.getAlertDialog(context, title, message, positiveMsg, negativeMsg, callback);
                    updateAlertDialog.setCanceledOnTouchOutside(false);
//                    alertDialog.getWindow().setBackgroundDrawableResource(R.color.colorDialogBgd);//设置Dialog背景透明效果
                } else {
                    updateAlertDialog.setTitle(title);
                    updateAlertDialog.setContent(message);
                }
                updateAlertDialog.show();
            }
        }

        @Override
        public MaterialDialog showProgressDialog(Context context, String message, MaterialDialog.SingleButtonCallback callback) {
            if (context != null) {
                if (progressDialog == null) {
                    progressDialog = DialogHelper.getProgressDialog(context, message, callback);
                    progressDialog.setCanceledOnTouchOutside(false);
//                    materialDialog.getWindow().setBackgroundDrawableResource(R.color.colorDialogBgd);//设置Dialog背景透明效果
                } else {
                    progressDialog.setContent(message);
                }
                progressDialog.show();
            }

            return progressDialog;

        }

        @Override
        public void showInformationDialog(  Context context,String title,
                                            String message, String positiveText, String negativeText, MaterialDialog.SingleButtonCallback callback) {
            if (context != null) {
                if (informationDialog == null) {
                    informationDialog = DialogHelper.getInformationDialog(context, title,message,positiveText,negativeText, callback);
                    informationDialog.setCanceledOnTouchOutside(false);
//                    materialDialog.getWindow().setBackgroundDrawableResource(R.color.colorDialogBgd);//设置Dialog背景透明效果
                } else {
                    informationDialog.setContent(message);
                }
                informationDialog.show();
            }
        }


        @Override
        public void hideInformationDialog() {
            if (informationDialog != null && informationDialog.isShowing()) {
                informationDialog.dismiss();
            }
        }

        @Override
        public void hideProgressDialog() {
            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }

        @Override
        public void hideUpdateAlertDialog() {
            if (updateAlertDialog != null && updateAlertDialog.isShowing()) {
                updateAlertDialog.dismiss();
            }
        }

        @Override
        public void hideWaitDialog() {
            if (materialDialog != null && materialDialog.isShowing()) {
                materialDialog.dismiss();
            }
        }

        @Override
        public void startActivity(Class<?> t) {
            startActivity(t, null);
        }

        @Override
        public void startActivity(Class<?> t, Bundle extra) {

            if (context != null) {
                Intent intent = new Intent(context, t);
                if (extra != null) {
                    intent.putExtras(extra);
                }

                if (activity != null) {
                    activity.startActivity(intent);
                } else if (fragment != null) {
                    fragment.startActivity(intent);
                }
            }


        }

        @Override
        public void startActivityForResult(Class<?> t, int requestCode) {
            startActivityForResult(t, requestCode, null);
        }

        @Override
        public void startActivityForResult(Class<?> t, int requestCode, Bundle extra) {

            if (context != null) {
                Intent intent = new Intent(context, t);
                if (extra != null) {
                    intent.putExtras(extra);
                }

                if (activity != null) {
                    activity.startActivityForResult(intent, requestCode);
                } else if (fragment != null) {
                    fragment.startActivityForResult(intent, requestCode);
                }
            }

        }

        @Override
        public void startFragment(Class<?> t) {
            startFragment(t, null);
        }

        @Override
        public void startFragment(Class<?> t, Bundle extra) {

            if (context != null) {
                Intent intent = new Intent(context, FragmentBackHolderActivity.class);
                intent.putExtra(FragmentBackHolderActivity.KEY_FRAGMENT_TYPE, t);
                if (extra != null) {
                    intent.putExtra(FragmentBackHolderActivity.KEY_FRAGMENT_DATA, extra);
                }

                if (activity != null) {
                    activity.startActivity(intent);
                } else if (fragment != null) {
                    fragment.startActivity(intent);
                }
            }
        }

        @Override
        public void startFragmentForResult(Class<?> t, int requestCode) {
            startFragmentForResult(t, requestCode, null);
        }

        @Override
        public void startFragmentForResult(Class<?> t, int requestCode, Bundle extra) {

            if (context != null) {
                Intent intent = new Intent(context, FragmentBackHolderActivity.class);
                intent.putExtra(FragmentBackHolderActivity.KEY_FRAGMENT_TYPE, t);
                if (extra != null) {
                    intent.putExtra(FragmentBackHolderActivity.KEY_FRAGMENT_DATA, extra);
                }

                if (activity != null) {
                    activity.startActivityForResult(intent, requestCode);
                } else if (fragment != null) {
                    fragment.startActivityForResult(intent, requestCode);
                }
            }
        }

        @Override
        public void handleError(int code, String message) {
            hideWaitDialog();

        }

        @Override
        public void hideKeyboard() {
            Activity context = null;

            if (activity != null) {
                context = activity;
            } else if (fragment != null) {
                context = fragment.getActivity();
            }

            if (context != null) {
                if (context.getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
                    if (context.getCurrentFocus() != null) {
                        InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                        inputManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(),
                                InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
            }
        }
    }

}
